﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ClientModel;
using SessionUserControl;
using ListViewItemCustomControl;
using LocalServerApp;

namespace Client
{
    /// <summary>
    /// Interaction logic for pagSession.xaml
    /// </summary>
    public partial class pagSession : Page, Observer
    {
        private static pagSession me;
        private static Frame frmMainContent;

        public static pagSession getInstance()
        {
            if (me == null)
                    me = new pagSession();
            return me;
        }

        private pagSession()
        {
            InitializeComponent();
        }

        public void dimMe()
        {
            grdContent.Opacity = .5;
            this.IsEnabled = false;
            this.Focusable = false;
        }

        public void unDimMe()
        {
            grdContent.Opacity = 1;
            this.IsEnabled = true;
            this.Focusable = true;
        }

       private void addIndleveringControl(Person p, int hangerNo)
       {
           SessionUserControl.UserControl1 depositControl = new SessionUserControl.UserControl1("Indlevering", p.Name, hangerNo);
           depositControl.HeaderBackground = Brushes.MediumSeaGreen;
           depositControl.addEventHandlerToBtnConfirm(new RoutedEventHandler(btnIndlever_Click));
           depositControl.addEventHandlerToBtnHanger(new RoutedEventHandler(btnEkstraHanger_Click));
           depositControl.addEventHandlerToBtnCancel(new RoutedEventHandler(btnCancelDeposit_Click));
           depositControl.Tag = p;

           wrpPnl.Children.Add(depositControl);
       }

       private void addUdleveringControl(Person p, int hangerNo)
       {
           SessionUserControl.UserControl1 withdrawControl = new SessionUserControl.UserControl1("Udlevering", p.Name, hangerNo);
           withdrawControl.HeaderBackground = Brushes.LightSteelBlue;
           withdrawControl.addEventHandlerToBtnConfirm(new RoutedEventHandler(btnUdlever_Click));
           withdrawControl.addEventHandlerToBtnHanger(new RoutedEventHandler(btnEkstraHanger_Click));
           withdrawControl.addEventHandlerToBtnCancel(new RoutedEventHandler(btnCancelWithdrawal_Click));
           withdrawControl.Tag = p;

           wrpPnl.Children.Add(withdrawControl);
       }

        private void removeCustomControls()
        {
            wrpPnl.Children.Clear();    
        }

        private bool isUserControlAdded(Person p)
        {
            bool found = false;
            int i = 0;
            Person person = null;

            while (i < wrpPnl.Children.Count && !found)
            {
                SessionUserControl.UserControl1 sessionCtrl = wrpPnl.Children[i] as SessionUserControl.UserControl1;

                if (sessionCtrl != null)
                {
                    person = sessionCtrl.Tag as Person;
                }

                if (person != null)
                {
                    if (p.Equals(person))
                    {
                        found = true;
                    }
                }

                i++;
            }

            return found;
        }

        public void addCustomControl(Person p)
        {
            if (!isUserControlAdded(p))
            {
                try
                {
                    if (!Service.getInstance().isInWardrobe(p.HashIdentifier))
                    {
                        addIndleveringControl(p, Service.getInstance().depositClothes(p));
                    }
                    else
                    {
                        foreach (int i in Service.getInstance().getClothesInWardrobe(p.HashIdentifier))
                        {
                            addUdleveringControl(p, i);
                        }
                    }
                }
                catch (System.ServiceModel.FaultException ee)
                {
                    addErrorMessage(ee.Message);
                }
                catch (Exception ex)
                {
                    addErrorMessage(ex.Message);
                }
            }
        }

        private void addErrorMessage(string errorMessage)
        {
            ListViewItemCustomControl.CustomControl1 lvi = new ListViewItemCustomControl.CustomControl1();
            lvi.Date = DateTime.Now.ToLongTimeString();
            lvi.TextBlock1 = errorMessage;
            lvi.BackgroundColor = Brushes.Red;
            listView1.Items.Insert(0, lvi);
        }

        private void btnIndlever_Click(object sender, RoutedEventArgs e)
        {
            Button btn = sender as Button;

            if(btn!=null)
            {
            SessionUserControl.UserControl1 usc = btn.Tag as SessionUserControl.UserControl1;

            if (usc != null)
            {
                try
                {
                   addEventMessage(usc, Brushes.MediumSeaGreen);   
                }
                catch (System.Exception ex)
                {
                    //handle exception
                    throw ex;
                }
            }
            }
        }

        private void btnUdlever_Click(object sender, RoutedEventArgs e)
        {
            Button btn = sender as Button;

            if(btn!=null)
            {
            SessionUserControl.UserControl1 usc = btn.Tag as SessionUserControl.UserControl1;

            if (usc != null)
            {
                try
                {
                    Service.getInstance().withdrawClothes(usc.HangerNo);
                    addEventMessage(usc, Brushes.LightSteelBlue);
                }
                catch (System.Exception ex)
                {
                    //handle exception
                    throw ex;
                }
            }

            }
        }

        private void addEventMessage(SessionUserControl.UserControl1 usc, SolidColorBrush color)
        {
            ListViewItemCustomControl.CustomControl1 lstvwItem = new ListViewItemCustomControl.CustomControl1();
            lstvwItem.Date = DateTime.Now.ToLongTimeString();
            lstvwItem.TextBlock1 = usc.GuestName;
            lstvwItem.TextBlock2 = "Bøjlenr.: " + usc.HangerNo;
            lstvwItem.BackgroundColor = color;

            listView1.Items.Insert(0, lstvwItem);

            wrpPnl.Children.Remove(usc);
        }

        #region Observer Members

        public void updateNewPerson(Person p)
        {
            if (p != null)
            {
                addCustomControl(p);
            }
            else
            {
                string error = "Kortet blev ikke registreret";
                addErrorMessage(error);
            }
        }

        #endregion

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {

        }

        private void btnSøg_Click(object sender, RoutedEventArgs e)
        {
            MainWindow.SetPage(pagSearch.getInstance());
        }

        private void btnEndSession_Click(object sender, RoutedEventArgs e)
        {
             MessageBoxResult objResult = new MessageBoxResult();
             objResult = MessageBox.Show("Er du sikker på at du ønsker at afslutte vagten?", "Bekræft", MessageBoxButton.YesNo, MessageBoxImage.Question);
             if (objResult == MessageBoxResult.Yes)
            {
                Service.getInstance().endSession();
            }
        }

        private void btnSettings_Click(object sender, RoutedEventArgs e)
        {
            MainWindow.SetPage(pagSettings.getInstance());
        }

        private void btnEkstraHanger_Click(object sender, RoutedEventArgs e)
        {
            Button btn = sender as Button;

            if(btn!=null)
            {
               SessionUserControl.UserControl1 usc = btn.Tag as SessionUserControl.UserControl1;
                
                if(usc!=null)
                {
                    Person p = usc.Tag as Person;
                     
                    if(p!=null)
                    {
                        try
                        {
                            int hangerNo = Service.getInstance().depositClothes(p);
                            addIndleveringControl(p,hangerNo);
                        }
                        catch (System.ServiceModel.FaultException ee)
                        {
                            addErrorMessage(ee.Message);
                        }
                        catch (Exception ex)
                        {
                            addErrorMessage(ex.Message);
                        }
                    }


                }
            }
        }

        private void btnCancelDeposit_Click(object sender, RoutedEventArgs e)
        {
            Button btn = sender as Button;
            
            if(btn!=null)
            {
              SessionUserControl.UserControl1 usc = btn.Tag as SessionUserControl.UserControl1;

            if (usc != null)
            {
                Person p = usc.Tag as Person;

                if (p != null)
                {
                    try
                    {
                        Service.getInstance().cancelDeposit(usc.HangerNo);
                        wrpPnl.Children.Remove(usc);
                    }
                    catch (System.Exception ex)
                    {
                        throw ex;
                    }

                }
            }
            }
        }

        private void btnCancelWithdrawal_Click(object sender, RoutedEventArgs e)
        {
            Button btn = sender as Button;
            
            if(btn!=null)
            {
                SessionUserControl.UserControl1 usc = btn.Tag as SessionUserControl.UserControl1;

              if (usc != null)
              {
                  wrpPnl.Children.Remove(usc);            
              }
            }
        }
    }
}